<?php
   require_once "model/Model.php";
   require_once "../common/connDB.php";
   class FoodModel extends Model{
     
   	 public function __construct(){
        parent::__construct();
   	 }
     public function getFoodList(){
      $sql="select * from food";
      $data=$this->conn->query($sql);
     	return $data;
     }
     //将菜品信息写入数据库
     public function addFoodInfo(){
      //上传图片文件 
     $pic=$this->uploadfile("pic",'../images/');
     //1.获取提交的内容
      $name=$_POST['name'];
      $price=$_POST['price'];
  
   $sql="insert into food values(null,'$name','$price','$pic')";
   echo $sql;
   $result=$this->conn->query($sql);//写入数据库
   return $result;
     }
     //更新菜品，获取菜品信息
    public function getFoodbyID(){
      $fid=$_GET['fid'];
   $sql="select * from food where fid=$fid";
      $data=$this->conn->query($sql);
      return $data;
    } 
    public function updateFoodInfo(){
      //上传图片文件 
     $pic=$this->uploadfile("pic",'../images/');
     //1.获取修改提交的内容
     $fid=$_POST['fid'];
  $name=$_POST['name'];
  $price=$_POST['price'];
  
   $sql="update food set name='{$name}',price='{$price}',pic='{$pic}' where fid=$fid";

   echo $sql;
   $result=$this->conn->query($sql);//写入数据库
   return $result;
    }
    public function delFoodInfo(){
          $fid=$_GET['fid'];
          $sql="select * from food where fid={$fid}";
          $result=mysql_query($sql);
          $row=mysql_fetch_assoc($result);
          $filename='../images/'.$row["pic"];
          $sql="delete from food where fid={$fid}";
          $result=$this->conn->query($sql);//写入数据库
          if(file_exists($filename)) unlink($filename);
          return $result;
        }
   }

?>